package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.service.goods.CategoryService;
import com.qingcheng.service.goods.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:boy
 * @Date:2019/7/6
 * @description:
 * @ModifiedBy:
 */
@RestController
@RequestMapping("/item")
public class ItemController {
    @Reference
    private SpuService spuService;
    @Reference
    private CategoryService categoryService;
    @Autowired
    private TemplateEngine templateEngine;
    @Value("${pagePath}")
    private String pagePath;

    @GetMapping("/createPage")
    public void createPage(String id) {
        Goods goods = spuService.findGoodsBySpuId(id);
        Spu spu = goods.getSpu();
        List<Sku> skuList = goods.getSkuList();
        Map urlMap = new HashMap();
        for (Sku sku : skuList) {
            //对规格参数排序
            if ("1".equals(sku.getStatus())) {
                String jsonSpec = JSON.toJSONString(JSON.parseObject(sku.getSpec()), SerializerFeature.MapSortField);
                urlMap.put(jsonSpec, sku.getId() + ".html");
            }
            Context context = new Context();
            Map<String, Object> model = new HashMap<String, Object>();
            //spu sku
            model.put("spu", spu);
            model.put("sku", sku);
            //分类信息
            List<String> categoryList = new ArrayList<String>();
            categoryList.add(categoryService.findById(spu.getCategory1Id()).getName());
            categoryList.add(categoryService.findById(spu.getCategory2Id()).getName());
            categoryList.add(categoryService.findById(spu.getCategory3Id()).getName());
            model.put("categoryList", categoryList);
            //图片
            model.put("skuImage", sku.getImage());
            model.put("skuImages", sku.getImages().split(","));
            model.put("spuImages", spu.getImages().split(","));
            //规格参数
            Map paraItems = JSON.parseObject(spu.getParaItems());
            model.put("paraItems", paraItems);
            Map specItems = JSON.parseObject(sku.getSpec());
            model.put("specItems", specItems);
            //规格面板
            Map<String, List> specMap = (Map) JSON.parseObject(spu.getSpecItems());
            for (String key : specMap.keySet()) {
                List<String> list = specMap.get(key);
                List<Map> mapList = new ArrayList<Map>();
                for (String value : list) {
                    Map map = new HashMap();
                    map.put("option", value);
                    //商品详情地址
                    Map spec = JSON.parseObject(sku.getSpec());
                    spec.put(key, value);
                    String specJson = JSON.toJSONString(spec, SerializerFeature.MapSortField);
                    map.put("url", urlMap.get(specJson));
                    //是否当前商品详情
                    if (value.equals(specItems.get(key))) {
                        map.put("checked", true);
                    } else {
                        map.put("checked", false);
                    }
                    mapList.add(map);
                }
                specMap.put(key, mapList);
            }
            model.put("specMap", specMap);

            //填充模型
            context.setVariables(model);
            //生成文件位置
            File file = new File(pagePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File destFile = new File(file, sku.getId() + ".html");
            try {
                PrintWriter writer = new PrintWriter(destFile, "UTF-8");
                templateEngine.process("item", context, writer);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

    }
}
